package com.demo.http;


import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class HttpServerDemo {
    @RequestMapping(value = "/server/submitJob", method = RequestMethod.POST, produces = {"text/plain", "application/*"})
    @ResponseBody
    public String submitJob(HttpEntity<String> requestEntity, HttpServletRequest request) throws IOException {
        System.out.println("server start ...................");

        String body = requestEntity.getBody();

        JobResult jobResult = JSON.parseObject(body,JobResult.class);
        System.out.println("type = " + jobResult.getId() + " info = " + jobResult.getErrDesc());

        HttpHeaders headers = requestEntity.getHeaders();


        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
        System.out.println(request.getRemoteAddr());
        return "true";
    }

    /**
     * workflow向worker提交任务
     *
     * @param requestEntity
     */
    @RequestMapping(value = "/jobSubmit", method = RequestMethod.POST, produces = {"text/plain", "application/*"})
    @ResponseBody
    public void processJobSubmit(HttpEntity<String> requestEntity) {

    }
}
